﻿using ImperialServer.Common.Entities;
using ImperialUtilities.Exceptions;
using ImperialUtilities.Utilities;
using ImperialWebSite.Content.Resources;
using ImperialWebSite.Controllers;
using ImperialWebSite.Presenters.Specie.Interfaces;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace ImperialWebSite.Presenters.Specie.Classes
{
    public class QuerySpeciesPresenter
    {
        private IQuerySpecies _view;
        private SpecieController _controller;
        private EventViewer _logger;

        /// <summary>
        /// Contructor de la clase
        /// </summary>
        /// <param name="view">Interfaz que enlaza con la vista</param>
        public QuerySpeciesPresenter(IQuerySpecies view)
        {
            _view = view;
            _controller = new SpecieController();
            _logger = new EventViewer("ImperialWebSite.Presenters.Specie.Classes");
        }

        /// <summary>
        /// Metodo que hace la carga de los especies existentes en la base de datos
        /// </summary>
        internal void LoadCurrentSpecies()
        {
            try
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.enterMessageInfo, "LoadCurrentSpecies"));

                DataTable dtSpecies = _controller.SelectAllSpecies();
                _view.SessionCurrentSpecies = dtSpecies;
                _view.SpeciesDataSource = dtSpecies;

                HttpContext.Current.Trace.Write(Messages.successfulTransactionMessage);
            }
            catch (ImperialException ie)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = ie.ImperialMessage;
            }
            catch (Exception e)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = Messages.failureTransactionMessage;
                _logger.WriteEventViewerLog("LoadCurrentSpecies", e.Message, true);
            }
            finally
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.exitMessageInfo, "LoadCurrentSpecies"));
            }
        }

        /// <summary>
        /// Metodo que ejecuta la llamada al controlador para actualizar una especie
        /// </summary>
        internal void UpdateSpecie()
        {
            SpecieEntity specie = null;
            bool isAlreadyRegistered = false;
            try
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.enterMessageInfo, "UpdateSpecie"));

                if (_view.SessionCurrentSpecies == null)
                    _view.SessionCurrentSpecies = _controller.SelectAllSpecies();

                if (!_controller.IsTheSameSpecie(_view.SpecieNameText, _view.SessionCurrentSpecies, _view.SessionCurrentSpecie))
                    isAlreadyRegistered = _controller.SpecieAlreadyRegistered(_view.SpecieNameText, _view.SessionCurrentSpecies);

                if (!isAlreadyRegistered)
                {
                    specie = _controller.CreateNewSpecie(_view.SpecieNameText);
                    specie.Id = int.Parse(_view.SessionCurrentSpecie.ToString());
                    if (_controller.UpdateSpecie(specie))
                    {
                        _view.SuccessfulTransaction = true;
                        _view.TransactionMessage = Messages.successfulTransactionMessage;
                    }
                }
                else
                {
                    _view.SuccessfulTransaction = false;
                    _view.TransactionMessage = Messages.duplicatedSpecieMessage;
                }

                HttpContext.Current.Trace.Write(Messages.successfulTransactionMessage);
            }
            catch (ImperialException ie)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = ie.ImperialMessage;
            }
            catch (Exception e)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = Messages.failureTransactionMessage;
                _logger.WriteEventViewerLog("UpdateSpecie", e.Message, true);
            }
            finally
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.exitMessageInfo, "UpdateSpecie"));
            }
        }
    }
}